package MULE.Model;

import java.awt.event.ActionEvent;

import MULE.Model.Logic.LandOfficeLogic;
import MULE.Model.Logic.MuleDriver;
import MULE.Model.Logic.MulePresenter;
import MULE.Model.Panel.LandOfficePanel;
import MULE.Model.Panel.MapPanel;
import MULE.Model.Panel.TownPanel;

public class LandOffice extends MulePresenter {

	public static final String dialogTitle = "Land Office";
	private LandOfficeLogic landOL;

	public enum ActionCommand {
		ENTER_LAND_OFFICE, EXIT_LAND_OFFICE, BUY_PROPERTY;
	}

	/**
	 * Creates a new Land Office responsible for processing/manipulating the
	 * given view.
	 * 
	 * @param view
	 *            The land office panel that this object controls.
	 */
	public LandOffice(LandOfficePanel view) {
		super(view);
		view.addExitButtonListener(this);
		LandOfficeLogic landOL = new LandOfficeLogic();
	}

	/**
	 * Calculates a pseudo-random price for land based on the game's current
	 * round.
	 * 
	 * @param currentRound
	 *            The current round of the game.
	 * @return Price of land at this time in the game.
	 */

	/**
	 * Allows the given player to purchase given tile for specified price.
	 * 
	 * @param p
	 *            The purchasing player.
	 * @param land
	 *            The land being purchased.
	 * @param cost
	 *            The cost of the land.
	 */
	public static void purchaseLand(Player p, ResourceTile land, Inventory cost) {
		// Deduct resources from player inventory.
		p.getInventory().subtract(cost);
		// Give land to player.
		land.setOwner(p);
	}

	/**
	 * Allows the given player to sell their tile for specified price.
	 * 
	 * @param p
	 *            The purchasing player.
	 * @param land
	 *            The land being sold.
	 * @param price
	 *            The price of the land.
	 */

	/**
	 * Event handler. Processes inputs from the land office panel.
	 * 
	 * @param e
	 *            The event which was fired.
	 */
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

		if (command.equals(ActionCommand.ENTER_LAND_OFFICE.toString())) {
			// Land office shows itself.
			MuleDriver.setGameView(view);
			MapPanel mapPanel = (MapPanel) MuleDriver.getMapLogic().getView();
			mapPanel.setResourceTileActionCommand(ActionCommand.BUY_PROPERTY
					.toString());
			mapPanel.setResourceTileListener(this);
		} else if (command.equals(ActionCommand.EXIT_LAND_OFFICE.toString())) {
			// Exit back to town.
			TownPanel townPanel = (TownPanel) MuleDriver.getTownLogic()
					.getView();
			MuleDriver.setGameView(townPanel);
		} else if (command.equals(ActionCommand.BUY_PROPERTY.toString())) {
			// Get tile and value it.
			ResourceTile tile = (ResourceTile) e.getSource();
			Player player = MuleDriver.getGameLogic().getCurrentPlayer();
			int currentRound = MuleDriver.getGameLogic().getCurrentRound();
			Inventory price = landOL.calcSellingPrice(currentRound);
			int moneyPrice = price.getAmount(Resource.MONEY);
			// Check tile ownership.
			if (tile.getOwner() == player) {
				// Sell player's tile.
				boolean sellConfirmed = MuleDriver.showConfirmDialog(
						dialogTitle, "Sell this land you own for $"
								+ moneyPrice + "?");
				if (sellConfirmed) {
					landOL.sellLand(player, tile, price);
					MuleDriver.updateScore(player);
				}
			} else {
				if (tile.getOwner() == null) {
					// Buy unowned tile.
					if (player.getInventory().hasResources(price)) {
						boolean buyConfirmed = MuleDriver.showConfirmDialog(
								dialogTitle, "Buy this land for $" + moneyPrice
										+ "?");
						if (buyConfirmed) {
							purchaseLand(player, tile, price);
							MuleDriver.updateScore(player);
						}
					} else {
						MuleDriver
								.showAlertDialog(dialogTitle,
										"You don't have enough resources to buy that land!");
					}
				} else {
					MuleDriver.showAlertDialog(dialogTitle,
							"Someone else owns that land!");
				}
			}
		}
	}
}
